<?php
/**
 * 公共类
 * Created by PhpStorm.
 * User: jiayanxiu
 * Date: 15/8/4
 * Time: 19:39
 */
class CommonApp {

    /**
     * 实例化对象
     * @var object
     * @author jyx
     * @date 2015/08/05
     */
    private $model;

    /**
     * 构造函数
     */
    public function __construct() {
        $this->model = m('goods');
    }
    /**
     * 商品库存为0时,已经卖完时，商品下架
     * @param $goods_id
     * @return bool
     * @author jyx
     * @date 2015/08/04
     */
    public function soldOut($goods_id) {
        $sql = "SELECT SUM(stock) AS total_stock FROM ".DB_PREFIX."goods_spec WHERE goods_id={$goods_id}";
        $tmp = $this->model->getRow($sql);
        if ($tmp['total_stock'] <= 0) {
            $sql = "UPDATE ".DB_PREFIX."goods SET if_show=0 WHERE goods_id={$goods_id}";
            $flag = $this->model->query($sql);
            if ($flag === false) {
                return false;
            }
        }
        return true;
    }

    /**
     * 获取商品库存总数量
     * @param $goods_id
     * @return mixed
     * @author jyx
     * @date 2015/07/17
     */
    public function getStock($goods_id) {
        $sql = "SELECT SUM(stock) AS total_stock FROM ".DB_PREFIX."goods_spec WHERE goods_id={$goods_id}";
        $row = $this->model->getRow($sql);
        return $row['total_stock'] ? intval($row['total_stock']) : 0;
    }

    /**
     * 全部替换掉后,这个函数删除
     * 获取用户与商品或商铺的关系
     * @param $user_id
     * @param $item_id
     * @param $type: goods或store
     * @return int
     * @author jyx
     * @date 2015/08/06
     */
    public function getRelationShip($user_id, $item_id, $type = 'goods') {
        $model = m('store');
        $sql = 'SELECT user_id FROM '.DB_PREFIX."collect WHERE user_id={$user_id} AND type='{$type}' AND item_id={$item_id}";
        $info = $model->getRow($sql);
        return $info ? 1 : 0;
    }

    /**
     * 获取用户与商品或商铺的关系
     * @param $model
     * @param $user_id
     * @param $item_id
     * @param string $type
     * @return int
     * @author jyx
     * @date 2015/11/30
     */
    public function getFollowShip($model, $user_id, $item_id, $type = 'goods') {
        $sql = 'SELECT user_id FROM '.DB_PREFIX."collect WHERE user_id={$user_id} AND type='{$type}' AND item_id={$item_id}";
        $info = $model->getRow($sql);
        return $info ? 1 : 0;
    }

    /**
     * 获取用户关注的商品或商铺的id,统计数据
     * @param $user_id
     * @param $type
     * @return array
     * @author jyx
     * @date 2015/09/17
     */
    public function getStatistics($user_id, $type) {
        $model = m('store');
        $tmp = $info = array();
        $sql = 'SELECT item_id FROM '.DB_PREFIX."collect WHERE user_id={$user_id} AND type='{$type}'";
        $tmp = $model->getAll($sql);
        foreach ($tmp as $key=>$val) {
            $info[$key] = $val['item_id'];
        }
        return $info;
    }

    /**
     * 添加商品或店铺的关注
     * @param $user_id
     * @param $item_id
     * @param string $type goods或store
     * @return bool
     * @author jyx
     * @date 2015/08/07
     */
    public function addFollow($user_id, $item_id, $type = 'goods') {
        $time = time();
        $sql = "INSERT INTO ".DB_PREFIX."collect (user_id,item_id,`type`,add_time) VALUES ({$user_id},{$item_id},'{$type}',{$time})";
        $rs = $this->model->query($sql);
        return $rs === false ? false : true;
    }

    /**
     * 取消商品或店铺的关注
     * @param $user_id
     * @param $item_id
     * @param string $type
     * @return bool
     * @author jyx
     * @date 2015/08/07
     */
    public function delFollow($user_id, $item_id, $type = 'goods') {
        $sql = "DELETE FROM ".DB_PREFIX."collect WHERE user_id={$user_id} AND item_id={$item_id} AND `type`='{$type}'";
        $rs = $this->model->query($sql);
        return $rs === false ? false : true;
    }

    /**
     * 插入日志记录
     * @param $uri
     * @param $content
     * @param $message
     * @param $type
     * @param int $user_id
     * @return bool
     * @author jyx
     * @date 2015/09/07
     */
    public function addLogs($uri, $content, $message, $type, $user_id = 0) {
        $time = time();
        $sql = "INSERT INTO ".DB_PREFIX."logs (uri,content, message, `type`, user_id,add_time) VALUES ('{$uri}','{$content}','{$message}',{$type},{$user_id},{$time})";
        $rs = $this->model->query($sql);
        return $rs === false ? false : true;
    }

    /**
     * 判断是否是本网站的图片
     * @param $image
     * @param $image_size
     * @return string
     * @author jyx
     * @date 2015/11/17
     */
    public function isMySelfImage($image, $image_size = '') {
        if (strpos($image, FARFETCH_URL) === false) {
            if (strpos($image, ZHAYAN_URL) === false) {
                if (strpos($image, YOOX_URL) === false) {
                    if (strpos($image, BXL_URL) === false) {
                        $image = $image ? QINIU_URL . $image . $image_size : '';
                    }
                }
            }
        }
        return $image;
    }

    /**
     * 当商品库存为0时清除购物车中的信息
     * @param $model
     * @param $spec_id
     * @return bool
     * @author jyx
     * @date 2015/11/24
     */
    public function dropCart($model, $spec_id) {
        $res = true;
        //如果商品库存为0,则删除购物车中的商品
        $sql = "SELECT stock FROM ".DB_PREFIX."goods_spec WHERE spec_id={$spec_id}";
        $spec = $model->getRow($sql);
        if ($spec['stock'] < 1) {
            $sql = "DELETE FROM ".DB_PREFIX."cart WHERE spec_id={$spec_id}";
            $res = $model->query($sql);
        }
        return $res;
    }

    /**
     * 是否活动
     * @param $is_act
     * @param $start_time
     * @param $end_time
     * @param bool|false $type
     * @return int
     * @author jyx
     * @date 2015/11/24
     */
    public function isAct($array, $type = true) {
        $time = time();
        $is_act=($array['is_act']?$array['is_act']:$array['is_show_discount']);
        if(($time > $array['act_start_time']) && ($time < $array['act_end_time'])){
            if ($type == true) {
                $flag = ($is_act == 1)?1:0;
            } else {//购物车中使用
                $flag = ($is_act == 2)?1:0;
            }
        }else{
            $flag = 0;
        }
        return $flag;
    }





}